﻿/**
* dataService - AngularJS module for web api management.
*
*
* Credits
* =======
*
* Copyright 2016 LonghronShen <LonghronShen@Hotmail.com>
*/

(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'ccp';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.service('dataService', ["$rootScope", "$http", "$q", "routeService", "exceptionHandleService", function ($rootScope, $http, $q, routeService, exceptionHandleService) {
        var checkMessageCode = function (response) {
            return true;
        };

        this.sayHello = function (name) {
            // console.info("Calling " + name);
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url: '/api/Common/SayHello?name=' + name
            }).then(function successCallback(response) {
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!" });
            });
            return deferred.promise;
        };

        this.login = function (email, password) {
            // console.info("Calling " + "dataService.login...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Account.Login.Url), { Email: email, Password: password }, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.deleteAccount = function (userId) {
            // console.info("Calling " + "dataService.deleteAccount...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Account.DeleteAccount.Url, { userId: userId }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.setAccountLockoutState = function (userId, value) {
            // console.info("Calling " + "dataService.setAccountLockoutState...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Account.SetAccountLockoutState.Url, { userId: userId, value: value }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.logout = function (token) {
            // console.info("Calling " + "dataService.logout...");
            var deferred = $q.defer();
            if (!token || token == "") {
                deferred.resolve({ message: "error!" });
            }
            $http.get(routeService.getUrl(routeService.Manage.Account.Logout.Url, { token: token }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getWebApiCredentials = function (startIndex, count) {
            // console.info("Calling " + "dataService.getWebApiCredentials...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.WebApiCredential.GetWebApiCredentials.Url, { startIndex: startIndex, count: count }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.addWebApiCredentials = function (appKey, appSecrect) {
            // console.info("Calling " + "dataService.addWebApiCredentials...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.WebApiCredential.AddWebApiCredentials.Url), { AppKey: appKey, AppSecrect: appSecrect }, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.removeWebApiCredential = function (id) {
            // console.info("Calling " + "dataService.removeWebApiCredential...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.WebApiCredential.RemoveWebApiCredential.Url, { id: id }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.getUsers = function (keyword, realName, phoneNumber, carCode, startTime, endTime, applicationStartTime, applicationEndTime, isEnabled, state, useCache, orderBy,acs,startIndex, count) {
            // console.info("Calling " + "dataService.getUsers...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.GetUsers.Url,
                {
                    keyword: keyword,
                    realName: realName,
                    phoneNumber:phoneNumber,
                    carCode: carCode,
                    startTime: startTime,
                    endTime: endTime,
                    applicationStartTime: applicationStartTime,
                    applicationEndTime: applicationEndTime,
                    isEnabled:isEnabled,
                    state: state,
                    useCache: useCache,
                    orderBy: orderBy,
                    acs:acs,
                    startIndex: startIndex,
                    count: count
                }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    if (state == 2 && keyword == "" && realName == "" && phoneNumber == "" && carCode == "" && startTime == "" && endTime == "" && applicationStartTime == "" && applicationEndTime == "" && isEnabled == "" && orderBy=="") {
                        $rootScope.$watch(function () { return response.data.Response.Output.Total; }, function (newValue, oldValue) {
                            $rootScope.$broadcast('total', response.data.Response.Output.Total);
                        });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getUserRoles = function () {
            // console.info("Calling " + "dataService.getUserRoles...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.GetUserRoles.Url, {}), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.createUser = function (email, phoneNumber) {
            // console.info("Calling " + "dataService.createUser...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.CreateUser.Url, { email: email, phoneNumber: phoneNumber }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getRealNameInfo = function (userId) {
            // console.info("Calling " + "dataService.getRealNameInfo...", userId);
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.GetRealNameInfo.Url, { userId: userId }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserInfo = function (userInfo) {
            // console.info("Calling " + "dataService.addWebApiCredentials...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.User.PutUserInfo.Url), userInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserRealNameInfo = function (userRealnameInfo) {
            // console.info("Calling " + "dataService.putUserRealNameInfo...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.User.PutUserRealNameInfo.Url), userRealnameInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserRealNameCertificationStatus = function (userRealnameCertificationInfo) {
            // console.info("Calling " + "dataService.putUserRealNameCertificationStatus...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.User.PutUserRealNameCertificationStatus.Url), userRealnameCertificationInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    exceptionHandleService.request(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.GetUserBasicInfo = function (userId) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "User/GetUserBasicInfo?userId={{userId}}", { userId: userId }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.ModifyUserInfoByUserId = function (userId, forceEdit, data) {
            var deferred = $q.defer();
            $http.post(Mustache.render("/api/" + "User/ModifyUserInfoByUserId?userId={{userId}}&forceEdit={{forceEdit}}", { userId: userId, forceEdit: forceEdit }), {
                Address: data.Address,
                AuthorizedLoad: data.AuthorizedLoad,
                CarLength: data.CarLength,
                CarNumber: data.CarNumber,
                CarriageType: data.CarriageType,
                CarType: data.CarType,
                HeadImage: data.HeadImage,
                PhoneNumber: data.PhoneNumber,
                Point: data.Point,
                //     简介
                Profile: data.Profile,
                RealName: data.RealName,
                RealNameState: data.RealNameState,
                UserId: data.UserId,
                UserName: data.UserName,
            })
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.ModifyRealNameInfoByUserId = function (userId, type, imageUri, forceEdit) {
            var deferred = $q.defer();
            $http.post(Mustache.render("/api/" + "User/ModifyRealNameInfoByUserId?userId={{userId}}&type={{type}}&imageUri={{imageUri}}&forceEdit={{forceEdit}}",
                {
                    userId: userId,
                    type: type,
                    imageUri: imageUri,
                    forceEdit: forceEdit
                }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.AquireStorageSharedSigniture = function (containerName) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Storage/AquireStorageSharedSigniture?containerName={{containerName}}&expireTime={{expireTime}}", { containerName: containerName }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetUserCommonLines = function (userId, skip, count) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/GetUserCommonLines?userId={{userId}}&startIndex={{startIndex}}&count={{count}}", { userId: userId, startIndex: skip, count: count }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetProvince = function () {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Common/GetProvince"))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetCity = function (provincecode) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Common/GetCity?provincecode={{provincecode}}", { provincecode: provincecode }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.DeleteCommonLine = function (lineId) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/DeleteCommonLine?lineId={{lineId}}", { lineId: lineId }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.AddUserCommonLine = function (userId,startingPlaceCode, destinationCode) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/AddUserCommonLine?userId={{userId}}&startingPlaceCode={{startingPlaceCode}}&destinationCode={{destinationCode}}",
            {
                userId: userId,
                startingPlaceCode: startingPlaceCode,
                destinationCode: destinationCode
            }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetUserFocusLines = function (userId) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/GetUserFocusLines?userId={{userId}}", { userId: userId }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.DeleteFocusLine = function (lineId) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/DeleteFocusLine?lineId={{lineId}}", { lineId: lineId }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.AddUserFocusLine = function (userId, startingPlaceCode, destinationCode) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "Line/AddUserFocusLine?userId={{userId}}&startingPlaceCode={{startingPlaceCode}}&destinationCode={{destinationCode}}",
            {
                userId: userId,
                startingPlaceCode: startingPlaceCode,
                destinationCode: destinationCode
            }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetUserBankCards = function (userId) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "BankCard/GetUserBankCards?userId={{userId}}", { userId: userId }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.DeleteUserBankCard = function (userId, bankCardNumber) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "BankCard/DeleteUserBankCard?userId={{userId}}&bankCardNumber={{bankCardNumber}}", { userId: userId, bankCardNumber: bankCardNumber }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.AddUserBankCard = function (userId, accountName, bankCardNumber) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "BankCard/AddUserBankCard?userId={{userId}}&accountName={{accountName}}&bankCardNumber={{bankCardNumber}}", { userId: userId, accountName: accountName, bankCardNumber: bankCardNumber }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetBankName = function (accountName, bankCardNumber) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "BankCard/GetBankName?accountName={{accountName}}&bankCardNumber={{bankCardNumber}}", { accountName: accountName, bankCardNumber: bankCardNumber }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

        this.GetUserRating = function (goodRating, userId, skip, count) {
            var deferred = $q.defer();
            $http.get(Mustache.render("/api/" + "User/GetUserRating?goodRating={{goodRating}}&userId={{userId}}&startIndex={{startIndex}}&count={{count}}", { goodRating: goodRating, userId: userId, startIndex: skip, count: count }))
            .then(function successCallback(response) {
                exceptionHandleService.request(response);
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.resolve({ message: "error!", response: response });
            });
            return deferred.promise;
        };

    }]);
})();